package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.PromotionPrice;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class UpgradeRecommendationDialog extends Dialog implements View.OnClickListener {

    private RecyclerView mRvList;
    private Button mBtnSubmit;
    private ImageView mIvClose;
    private BaseQuickAdapter<PromotionPrice, BaseViewHolder> mAdapter;
    private String type;
    private String money;

    public UpgradeRecommendationDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_upgrade_recommendation);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {

        mRvList = findViewById(R.id.rv_list);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
        mIvClose = findViewById(R.id.iv_close);
        mIvClose.setOnClickListener(this);
        mAdapter = new BaseQuickAdapter<PromotionPrice, BaseViewHolder>(R.layout.item_upgrade) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, PromotionPrice item) {
                TextView tvMoney = helper.getView(R.id.tv_money);
                tvMoney.setText(String.format("￥%s", item.getPresentPrice()));
                if (item.getCard().equals("金卡推广")) {
                    helper.setText(R.id.tv_type, String.format("升级金卡%s", item.getTypeName()));
                    helper.setBackgroundRes(R.id.tv_money, R.drawable.bg_gold_money);
                    helper.setBackgroundRes(R.id.ll_type, R.drawable.bg_gold_type);
                    helper.setTextColor(R.id.tv_money, Color.parseColor("#745D00"));
                } else {
                    helper.setText(R.id.tv_type, String.format("续费银卡%s", item.getTypeName()));
                    helper.setBackgroundRes(R.id.tv_money, R.drawable.bg_sliver_money);
                    helper.setBackgroundRes(R.id.ll_type, R.drawable.bg_sliver_type);
                    helper.setTextColor(R.id.tv_money, Color.parseColor("#000000"));
                }

                helper.setImageResource(R.id.iv_check, item.isCheck() ? R.mipmap.ic_checked : R.mipmap.ic_unchecked);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (PromotionPrice datum : mAdapter.getData()) {
                    datum.setCheck(false);
                }
                mAdapter.getData().get(position).setCheck(true);
                mAdapter.notifyDataSetChanged();
                type = mAdapter.getData().get(position).getType();
                money = mAdapter.getData().get(position).getPresentPrice();
            }
        });
    }

    @Override
    public void onClick(View view) {
        dismiss();
        if (view.getId() == R.id.btn_submit) {
            listener.onClick(type, money);
        }
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }

    public void setNewData(List<PromotionPrice> data) {
        mAdapter.setNewData(data);
        type = data.get(0).getType();
        money = data.get(0).getPresentPrice();
    }
}
